Remote repositories

Remote repositories like GitHub allow you to store you code, data, and other information in a convenient online location. However, most of the time you will want to work on your personal computer and take advantage of a nicer programming interface, or so-called integrated development environment (IDE) like RStudio. Using remotes also allows for easier collaboration among people working in different places. This lesson will introduce you to working with remote repositories and show how to move your files back and forth from the cloud to a local computer. In general, our model for this process looks something like this.


Create a project in RStudio

There are several ways to connect a remote repository to your local computer. Here we will see how to do so from within RStudio. RStudio provides a nice interface for using Git for version control via “Projects”. Note that these are different than the GitHub Projects we learned about here.

  1. Navigate to the testing repo we created on GitHub in the lesson on Intro to GitHub. Highlight and copy the full URL from your browser’s address bar.

  1. Start RStudio

  2. Select File –> New Project…, which will bring up a dialogue box.

  3. Select Version Control


  1. Select Git


  1. In the Repository URL box, copy/paste the full repo address from GitHub (note that YOUR_USERNAME in the location below should be your GitHub user name):


  1. Tab to the next box Project directory name, which should auto-fill with the repo name testing.

  2. Select the directory location where you’d like this to live. I strongly suggest choosing a location that is not under some other version control software such as Dropbox.


  1. Check the box in the lower left that says “Open in new session” and then click on the Create Project button.


  1. RStudio will open a new instance with all of the project files listed. You are now ready to do work with your project in RStudio. (Note that the files shown in your project may look different than the image below.)


Working on remotes from RStudio

There are several things to note here in RStudio (note that your views may differ depending on how you have the window panes set up). In the Files tab, you should see a README.md file plus the R and data folders we that we previously created from within GitHub. In addition, you should see a .gitignore and a testing.Rproj file.


Click on the .gitignore file to display its contents in the code pane. You should see 4 file extensions listed there:

.Rproj.user
.Rhistory
.RData
.Ruserdata


This .gitignore file contains information about the things that you don’t want Git to pay attention to when tracking different versions of your files and folder contents. For example, some people want to exclude .html files that are generated by .Rmd source files, or environment files unique to R and RStudio. You can see a list of common options for R here.


Using Git from RStudio

Now locate the Git tab in the pane with other tabs for Environment, History, etc. and click on it. You should see both the .gitignore and testing.Rproj files listed there with an empty box and 2 yellow question marks to the left of each of their names. This is the Git staging area and Git shows these 2 files as new (i.e., it’s a graphical means for showing the results of git status).


Add a file to staging

Go ahead and click the check box in the column marked “Staged” to the left of the .gitignore file and you will see that a green A appears in the column marked “Status”. This is equivalent to typing git add .gitignore via command-line Git.

Commit a file

Now click on the “Commit” button just above the file name, which will bring up a new commit window.



There are several important things to pay attention to here. The first is the menu bar across the top, which has buttons for “Changes” and “History” in the upper left, and “Pull” and “Push” in the upper right. At the moment, the “Changes” button is active, so you can see the contents of the .gitignore file displayed below in green. In this case, the file is brand new to Git, so everything is highlighted in green. Later you will see how this changes.


Now go ahead and click on the “History” button just to the left of the “Changes” button, which will bring up a log of your commit history. In the example below, the last file committed was ex_data.csv that we had previously created and committed directly from GitHub. You can see information about the the commit author, the date, and the first 7 characters from the unique commit SHA.


Now click back on the “Changes” tab and type a short but informative commit message in the box in the upper right. When you are finished, click the Commit button in the lower right. This is the equivalent of typing git commit -m "added gitignore" via command-line Git.


RStudio now pops open a commit window that displays the commit command and the top and the resulting reply from Git:

[main d20897b] added gitignore
 1 file changed, 4 insertions(+)
 create mode 100644 .gitignore


Go ahead and click the “Close” button in the upper right, which will return you to the main commit window. Close that window, which will return you to the main RStudio viewer. Note that now the .gitignore file has disappeared from the Git tab, and only the testing.Rproj file remains.